using System;
using System.Collections.Generic;

using TrueCode.Core.Aspects;

namespace TrueCode.Core
{
  public class LiteEqualityComparer< T > : IEqualityComparer< T >
  {
    private readonly Func< T, T, bool > _comparison;
    [CanBeNull] 
    private readonly Func< T, int > _getHashCode;

    public LiteEqualityComparer( [NotNull] Func< T, T, bool > comparison, [CanBeNull] Func< T, int > getHashCode = null )
    {
      if( comparison == null ) throw new ArgumentNullException( "comparison" );

      _comparison = comparison;
      _getHashCode = getHashCode;
    }

    public bool Equals( T x, T y )
    {
      return _comparison( x, y );
    }

    public int GetHashCode( T obj )
    {
      if( _getHashCode == null )
        throw new NotSupportedException( "getHashCode function is not provided" );
      return _getHashCode( obj );
    }
  }
}